package com.global.model;

import java.io.Serializable;
import javax.persistence.*;

/**
 * The persistent class for the ZLY_ST_BSC_DSOD database table.
 * @author zly601436
 */
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate=true)
@Table(name = "ZLY_ST_BSC_DSOD", uniqueConstraints = @UniqueConstraint(columnNames = {"DSO_ID", "ALIAS"}))
@SequenceGenerator(name="ZLY_ST_BSC_DSOD_ID_GENERATOR", sequenceName="ZLY_ST_BSC_DSOD_ID_SEQ", initialValue=1000, allocationSize=1)
@NamedQueries({
        @NamedQuery(
                name = "findDataSourceXColumnByAlias",
                query = "select r from DataSourceXColumn r where upper(r.alias) = :uniqueAlias "
        ),
        @NamedQuery(
                name = "findDataSourceXColumnByDataSourceX",
                query = "select r from DataSourceXColumn r where r.dataSourceX.id = :dataSourceXId order by r.displayIndex "
        )
})
public class DataSourceXColumn extends BaseObject implements Serializable {

	private static final long serialVersionUID = 6545245551988921928L;
	
	private Long id;
	private String alias;
	private String description;
	private int displayIndex;
	
	private String dbColumnTypeName;
	private DataSourceX dataSourceX;
	
    /**
     * 定义操作人、操作时间
     */
    private Who who;
    
    public DataSourceXColumn() {
    }

    public DataSourceXColumn(DataSourceX dataSource) {
    	this.dataSourceX = dataSource;
    }
    
	@Id	
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ZLY_ST_BSC_DSOD_ID_GENERATOR")
	public Long getId() {
		return this.id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 返回数据源中的字段别名
	 * @return String
	 */
	@Column(name="ALIAS", nullable=false, length=128)
	public String getAlias() {
		return this.alias;
	}
	/**
	 * 设置数据源中的字段别名
	 * @param name
	 */
	public void setAlias(String alias) {
		this.alias = (alias==null ? "" : alias.trim().toUpperCase());
	}

	/**
	 * 返回数据源中的字段描述
	 * @return String
	 */
	@Column(name="DESCRIPTION", nullable=false, length=1024)
	public String getDescription() {
		return this.description;
	}
	/**
	 * 设置数据源中的字段描述
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = (description==null ? "" : description.trim());
	}
	
	/**
	 * 返回显示顺序
	 * @return int
	 */
	@Column(name="DISPLAYINDEX", nullable=false)
	public int getDisplayIndex() {
		return displayIndex;
	}
	/**
	 * 设置显示顺序
	 * @param index
	 */
	public void setDisplayIndex(int index) {
		this.displayIndex = index;
	}

	/**
	 * 返回数据源中的字段的SQL数据类型名称
	 * @return String
	 */
	@Column(name="COLUMNTYPENAME", nullable=false, length=255)
	public String getDbColumnTypeName() {
		return this.dbColumnTypeName;
	}
    /**
     * 设置数据源中的字段的SQL数据类型名称
     * @param columnTypeName
     */
	public void setDbColumnTypeName(String columnTypeName) {
		this.dbColumnTypeName = columnTypeName;
	}	

	/**
	 * 返回数据源
	 * @return DataSourceX
	 */
	//bi-directional many-to-one association to DataSouceX
    @ManyToOne
	@JoinColumn(name="DSO_ID", nullable=false)
	public DataSourceX getDataSourceX() {
		return this.dataSourceX;
	}
	public void setDataSourceX(DataSourceX dataSourceX) {
		this.dataSourceX = dataSourceX;
	}

	/**
	 * 数据源字段配置是否合法
	 * <br/>如果显示顺序不大于0，则表示配置不合法
	 * @return true 表示合法
	 */
	@Transient
	public boolean isActive() {
		return displayIndex>0;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Who getWho() {
		return who;
	}
	/**
     * {@inheritDoc}
     */
	@Override
	public void setWho(Who who) {
		this.who = who;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((alias == null) ? 0 : alias.hashCode());
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		
		DataSourceXColumn other = (DataSourceXColumn) obj;
		
		if (id != other.id){
			if (alias == null) {
				if (other.alias != null)
					return false;
			} else if (!alias.equalsIgnoreCase(other.alias)){
				return false;
			}
		}
		
		return true;
	}

	@Override
	public String toString() {
		return "DataSourceXColumn [id=" + id + ", alias=" + alias + ", description=" + description + "]";
	}
}